package com.ziyun.erp.modules.material.service.impl;

import com.ziyun.erp.common.e.material.MaterialUnitEnum;
import com.ziyun.erp.common.e.purchase.PurchaseApplyStatusEnum;
import com.ziyun.erp.common.utils.JsonUtils;
import com.ziyun.erp.modules.purchase.entity.StoreMaterialSupplierEntity;
import com.ziyun.erp.modules.purchase.service.StoreMaterialSupplierService;
import com.ziyun.erp.modules.supp.entity.SellerInfoEntity;
import com.ziyun.erp.modules.supp.service.SellerInfoService;
import io.renren.common.utils.R;
import io.renren.common.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import com.ziyun.erp.modules.material.dao.MaterialCategoryDao;
import com.ziyun.erp.modules.material.entity.MaterialCategoryEntity;
import com.ziyun.erp.modules.material.service.MaterialCategoryService;


@Service("materialCategoryService")
public class MaterialCategoryServiceImpl extends ServiceImpl<MaterialCategoryDao, MaterialCategoryEntity> implements MaterialCategoryService {
    @Autowired
    private StoreMaterialSupplierService storeMaterialSupplierService;

    @Autowired
    private SellerInfoService sellerInfoService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<MaterialCategoryEntity> page = this.selectPage(
                new Query<MaterialCategoryEntity>(params).getPage(),
                new EntityWrapper<MaterialCategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<MaterialCategoryEntity> queryAllList() {
        return this.selectList(new EntityWrapper<MaterialCategoryEntity>()
                                    .eq("`delete`",0)
                                    .orderBy("category_order",true));
    }

    @Override
    public void deleteUpdate(Integer categoryId) {
        baseMapper.deleteUpdate(categoryId);
    }

    @Override
    public List<MaterialCategoryEntity> queryListParentId(Integer categoryId) {
        return baseMapper.selectList(new EntityWrapper<MaterialCategoryEntity>().eq("parent_id",categoryId));
    }

    @Override
    public List<MaterialCategoryEntity> queryList(HashMap<String, Object> stringObjectHashMap) {
        return null;
    }

    @Override
    public String selectChildrenCategoryId(String categoryIdStr) {
        if(StringUtil.isNull(categoryIdStr)){
            return baseMapper.selectChildrenCategoryId(",");
        } else {
            Integer categoryId = Integer.parseInt(categoryIdStr);
            if(categoryId ==  0){
                return baseMapper.selectChildrenCategoryId(",");
            } else {
                return  baseMapper.selectChildrenCategoryId(","+categoryIdStr);
            }
        }
    }

    @Override
    public void insertMaterialCategory(MaterialCategoryEntity materialCategory) {
        baseMapper.insert(materialCategory);
        materialCategory.setCategoryPath(materialCategory.getCategoryPath()+","+materialCategory.getCategoryId());
        baseMapper.updateById(materialCategory);
    }

    @Override
    public R getTree() {
        List<Map<String,Object>> materialCategoryTree = new ArrayList<>();

        //一级分类
        List<MaterialCategoryEntity> firstList = baseMapper.selectList(
                new EntityWrapper<MaterialCategoryEntity>()
                        .eq("`delete`",0)
                        .eq("level",1)
                        .eq("`parent_id`",0)
        );
        for (MaterialCategoryEntity first:firstList){
            Map<String,Object> firstMap = new HashMap<>();
            firstMap.put("id",first.getCategoryId());
            firstMap.put("label",first.getCategoryName());

            List<Map<String,Object>> children = new ArrayList<>();

            //二级分类
            List<MaterialCategoryEntity> secondList = baseMapper.selectList(
                    new EntityWrapper<MaterialCategoryEntity>()
                            .eq("`delete`",0)
                            .eq("level",2)
                            .eq("`parent_id`",first.getCategoryId())
            );
            for (MaterialCategoryEntity second:secondList){
                Map<String,Object> secondMap = new HashMap<>();
                secondMap.put("id",second.getCategoryId());
                secondMap.put("label",second.getCategoryName());

                List<Map<String,Object>> secondChildren = new ArrayList<>();

                //三级分类
                List<MaterialCategoryEntity> thirdList = baseMapper.selectList(
                        new EntityWrapper<MaterialCategoryEntity>()
                                .eq("`delete`",0)
                                .eq("level",3)
                                .eq("`parent_id`",second.getCategoryId())
                );
                for (MaterialCategoryEntity third:thirdList){
                    Map<String,Object> thirdMap = new HashMap<>();
                    thirdMap.put("id",third.getCategoryId());
                    thirdMap.put("label",third.getCategoryName());
                }
                secondMap.put("children",secondChildren);
                children.add(secondMap);
            }
            firstMap.put("children",children);
            materialCategoryTree.add(firstMap);
        }

        // 采购供应商列表
        List<StoreMaterialSupplierEntity> materialSupplier = storeMaterialSupplierService.selectList(
                new EntityWrapper<StoreMaterialSupplierEntity>()
                        .eq("`del`",0)
                        .eq("status",1)
        );

        // 门店列表
        List<SellerInfoEntity> getSelectMD = sellerInfoService.getSelectList();


        return R.ok()
                .put("materialCategoryTree",materialCategoryTree)
                .put("materialSupplier",materialSupplier)
                .put("getSelectMD",getSelectMD)
                .put("purchaseApplyStatusEnum", JsonUtils.enumToList(PurchaseApplyStatusEnum.class))
                .put("materialUnitEnum", JsonUtils.enumToList(MaterialUnitEnum.class));
    }

    @Override
    public Integer getlevel(String parentName) {
        return baseMapper.getlevelValue(parentName);
    }
}
